<?php

namespace common\models\company;

use Yii;

/**
 * This is the model class for table "{{%company}}".
 *
 * @property int $id
 * @property int $parent_id 上级管理单位ID
 * @property string $name 公司单位名称
 * @property string $abbr 公司单位简称或别名
 * @property string $province 省
 * @property string $city 市
 * @property string $district 区
 * @property string $address_detail 详细地址
 * @property string $telephone 联系电话
 * @property string $secret 单位密钥
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Company extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%company}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['parent_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'province', 'city', 'district', 'address_detail', 'telephone', 'secret'], 'required'],
            [['name', 'address_detail'], 'string', 'max' => 100],
            [['secret'], 'string', 'max' => 255],
            [['abbr', 'province', 'city', 'district'], 'string', 'max' => 50],
            [['telephone'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'parent_id' => '上级管理单位ID',
            'name' => '公司单位名称',
            'abbr' => '公司单位简称或别名',
            'province' => '省',
            'city' => '市',
            'district' => '区',
            'address_detail' => '详细地址',
            'telephone' => '联系电话',
            'secret' => '单位密钥',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getEncryptSecret() {
        /* @var $security \common\base\Security */
        $security = Yii::$app->security;
        $password = $security->decryptMaster($this->secret);
        return $security->encryptDataByKey($password);
    }

    public function fields() {
        return [
            'id',
            'parent_id',
            'name',
            'abbr',
            'province',
            'city',
            'district',
            'address_detail',
            'telephone',
            'secret' => 'encryptSecret',
        ];
    }

}
